package pack

import (
	"encoding/json"
	"mall_admin_api/biz/model"
	"mall_admin_api/biz/model/admin_product_specifications"
)

const (
	PriceColumn       = "price"
	CostPriceColumn   = "cost_price"
	MarketPriceColumn = "market_price"
	StockColumn       = "stock"
	WeightColumn      = "weight"
	PicColumn         = "pic"
)

func GetProductSpecificationsList(list []*model.ProductSpecificationsTemplate) []*admin_product_specifications.Specifications {
	var data = make([]*admin_product_specifications.Specifications, 0)
	if len(list) == 0 {
		return data
	}
	for _, item := range list {
		if tmp := ProductSpecifications(item); tmp != nil {
			data = append(data, tmp)
		}
	}
	return data
}

func ProductSpecifications(data *model.ProductSpecificationsTemplate) *admin_product_specifications.Specifications {
	var info = admin_product_specifications.NewSpecifications()
	if data == nil {
		return info
	}
	info.ID = data.ID
	info.Name = data.Name
	var value = make([]*admin_product_specifications.SpecificationsValue, 0)
	_ = json.Unmarshal([]byte(data.Value), &value)
	info.Value = value
	if !data.CreatedAt.IsZero() {
		info.CreateTime = data.CreatedAt.UnixMilli()
	}

	return info
}

func DefaultHeaders() []*admin_product_specifications.FormatSpecificationsHeader {
	var keys = []struct {
		k string
		v string
	}{
		{k: "图片", v: PicColumn},
		{k: "售价", v: PriceColumn},
		{k: "成本价", v: CostPriceColumn},
		{k: "市场价", v: MarketPriceColumn},
		{k: "库存", v: StockColumn},
		{k: "重量(kg)", v: WeightColumn},
		{k: "操作", v: "action"},
	}
	var data = make([]*admin_product_specifications.FormatSpecificationsHeader, 0)
	for _, v := range keys {
		data = append(data, &admin_product_specifications.FormatSpecificationsHeader{
			Title:    v.k,
			Align:    "center",
			Slot:     v.v,
			MinWidth: 140,
		})
	}
	return data
}

func DefaultValue() map[string]interface{} {
	return map[string]interface{}{
		PriceColumn:       float64(0),
		CostPriceColumn:   float64(0),
		MarketPriceColumn: float64(0),
		StockColumn:       "",
		WeightColumn:      "",
		PicColumn:         "",
	}
}
